<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {


	public function beforeFilter() {
		parent::beforeFilter();
		//Grant access to all the methods in this controller
		//$this->Auth->allow();
	}

//Login
	public function login() {
		if ($this->request->is('post')) {
			if ($this->Auth->login()) {
				$loggedUser = $this->Auth->user();
				if($loggedUser['Group']['id'] == 3) {
					return $this->redirect(array('controller'=>'clients', 'action' => 'historial',$loggedUser['Client']['id']));	
				} else {
					return $this->redirect($this->Auth->redirect());
				}
				
			}
			$this->Session->setFlash('Usuario o contraseña incorrectos.');
		}
	}

//Logout
	public function logout(){
		//$this->Session->setFlash('Good-Bye');
		$this->Auth->logout();
		$this->redirect('/');
		//$this->redirect('http://www.geministaller.com/car/clientes');
	}

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->User->recursive = 0;
		$loggedUser = $this->Auth->user();
		$this->set('group',$loggedUser['Group']['id']);
		$this->set('users', $this->paginate());
		$this->set('userlogin', $this->Auth->user());
	}

/**
 * AdminIndex method
 *
 * @return void
 */
	public function adminIndex() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
		$this->set('userlogin', $this->Auth->user());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'), 'default', array(), 'good');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
		$groups = $this->User->Group->find('list');
		$clients = $this->User->Client->find('list',array('order'=>'Client.nombre asc'));
		$this->set(compact('groups', 'clients'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list');
		$clients = $this->User->Client->find('list');
		$this->set(compact('groups', 'clients'));
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->User->delete()) {
			$this->Session->setFlash(__('User deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('User was not deleted'));
		$this->redirect(array('action' => 'index'));
	}




public function initDB() {
    $group = $this->User->Group;
    //Allow admins to everything
    $group->id = 1;
    $this->Acl->allow($group, 'controllers');

    
    $group->id = 2;
    $this->Acl->deny($group, 'controllers');
    $this->Acl->allow($group, 'controllers/Clients');
    $this->Acl->allow($group, 'controllers/Users');
    $this->Acl->allow($group, 'controllers/Services');
    $this->Acl->allow($group, 'controllers/Cars');
    $this->Acl->allow($group, 'controllers/Pages');

  
    $group->id = 3;
    $this->Acl->deny($group, 'controllers');
    $this->Acl->allow($group, 'controllers/Users/logout');
    $this->Acl->allow($group, 'controllers/Users/login');
    $this->Acl->allow($group, 'controllers/Clients/historial');
    $this->Acl->allow($group, 'controllers/Clients/edit');
    $this->Acl->allow($group, 'controllers/Cars/servicesHistory');


    //we add an exit to avoid an ugly "missing views" error message
    echo "all done";
    exit;
}



}
